package com.zja.www.dao.impl;

import com.zja.www.dao.DoctorDao;
import com.zja.www.entity.Doctor;
import com.zja.www.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorDaoImpl implements DoctorDao {

    private  final Connection connection;

    public DoctorDaoImpl() {
        this.connection = DBUtil.getConnection();
        if (this.connection == null) {
            throw new RuntimeException("无法获取数据库连接");
        }

    }

    @Override
    public Doctor getDoctorByName(String name) {
         String sql = "select * from doctor where name=?";
         try(PreparedStatement ps = connection.prepareStatement(sql)){
             ps.setString(1, name);
             ResultSet rs = ps.executeQuery();
             if(rs.next()){
                 Doctor doctor = new Doctor();
                 doctor.setId(rs.getInt("id"));
                 doctor.setName(rs.getString("name"));
                 doctor.setPassword(rs.getString("password"));
                 doctor.setDepartment(rs.getString("department"));
                 doctor.setDescription(rs.getString("description"));
                 doctor.setEmail(rs.getString("email"));
                 doctor.setPrice(rs.getInt("price"));
                 doctor.setPosition(rs.getString("position"));
                 return doctor;
             }
         }catch(SQLException e){
             e.printStackTrace();
         }
        return null;
    }

    @Override
    public boolean insert(Doctor doctor) {
         String sql = "INSERT INTO doctor (name, passwoed, department, description, email, price, position, department_id) VALUES (?, ?, ?, ?, ?, ?, ?)";
         try(PreparedStatement ps = connection.prepareStatement(sql)){
             ps.setString(1, doctor.getName());
             ps.setString(2, doctor.getPassword());
             ps.setString(3, doctor.getDepartment());
             ps.setString(4, doctor.getDescription());
             ps.setString(5, doctor.getEmail());
             ps.setDouble(6, doctor.getPrice());
             ps.setString(7, doctor.getPosition());
             ps.setInt(8, doctor.getDepartmentId());

             int rowsAffcted = ps.executeUpdate();
             return rowsAffcted > 0;

         }catch(SQLException e){
             e.printStackTrace();
         }

        return false;
    }

    @Override
    public Doctor getDoctorById(int id) {
        String sql = "SELECT * FROM doctor WHERE id = ? ";
        try(PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);

            try(ResultSet rs = ps.executeQuery()) {
                if(rs.next()) {
                    Doctor doctor = new Doctor();
                    doctor.setId(rs.getInt("id"));
                    doctor.setName(rs.getString("name"));
                    doctor.setPassword(rs.getString("password"));
                    doctor.setDepartment(rs.getString("department"));
                    doctor.setDescription(rs.getString("description"));
                    doctor.setEmail(rs.getString("email"));
                    doctor.setPrice(rs.getDouble("price"));
                    doctor.setPosition(rs.getString("position"));
                    doctor.setDepartmentId(rs.getInt("department_id"));
                    return doctor;
                }
            }

        }catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    public List<Doctor> getAllDoctors() {
         //初始化医生列表
         List<Doctor> doctors = new ArrayList<Doctor>();

         String sql = "SELECT * FROM doctor";

         try(PreparedStatement ps = connection.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()){
             Doctor doctor = new Doctor();
             doctor.setId(rs.getInt("id"));
             doctor.setName(rs.getString("name"));
             doctor.setPassword(rs.getString("passwoed"));
             doctor.setDepartment(rs.getString("department"));
             doctor.setDescription(rs.getString("description"));
             doctor.setEmail(rs.getString("email"));
             doctor.setPrice(rs.getDouble("price"));
             doctor.setPosition(rs.getString("position"));
             doctor.setDepartmentId(rs.getInt("department_id"));
             doctors.add(doctor);

         }catch(SQLException e){
             e.printStackTrace();
         }
        return doctors;
    }



    @Override
    public boolean updateDoctor(Doctor doctor) {
        String sql = "update doctor set name = ?, password = ?, department = ?, description = ?, email = ?, price = ?, position = ?, department_id = ? where id = ?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setString(1, doctor.getName());
            ps.setString(2, doctor.getPassword());
            ps.setString(3, doctor.getDepartment());
            ps.setString(4, doctor.getDescription());
            ps.setString(5, doctor.getEmail());
            ps.setDouble(6, doctor.getPrice());
            ps.setString(7, doctor.getPosition());
            ps.setInt(8, doctor.getDepartmentId());
            ps.setInt(9, doctor.getId());

            int rowsAffcted = ps.executeUpdate();
            return rowsAffcted > 0;

        }catch(SQLException e){
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public boolean deleteDoctor(int id) {
         String sql = "delete from doctor where id = ?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setInt(1, id);

            int rowsAffcted = ps.executeUpdate();
            return rowsAffcted > 0;

        }catch(SQLException e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Doctor> getDoctorByDepartmentId(int departmentId) {
         List<Doctor> doctors = new ArrayList<>();

         String sql = "SELECT * FROM doctor WHERE department_id = ? LIMIT 1";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setInt(1, departmentId);

            try(ResultSet rs = ps.executeQuery()) {
                while(rs.next()) {
                    Doctor doctor = new Doctor();
                    doctor.setId(rs.getInt("id"));
                    doctor.setName(rs.getString("name"));
                    doctor.setPassword(rs.getString("password"));
                    doctor.setDepartment(rs.getString("department"));
                    doctor.setDescription(rs.getString("description"));
                    doctor.setEmail(rs.getString("email"));
                    doctor.setPrice(rs.getDouble("price"));
                    doctor.setPosition(rs.getString("position"));
                    doctor.setDepartmentId(rs.getInt("department_id"));
                    doctors.add(doctor);

                     }
                }
             }catch(SQLException e){
            e.printStackTrace();
        }
        return doctors;
    }
}
